<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/12
 * Time: 17:06
 */

namespace Modules\Admin\Http\Controllers;


use Illuminate\Support\Facades\Request;
use HiCommon\Service\RongService;
use Illuminate\Support\Facades\Cache;
class JpushController extends BaseController
{

    public function jpush(){
        return view("admin::jpush.index");
    }

    //融云聊天
    public function rongyun_im(){

//        $arr['targetId'] = '"xinda5ebbaa02e74b4"';
//        $arr['sentTime'] = '1589453764231';
//        $arr['content'] = 'gfdgfdshfghgfjfg';
//
//        $admin_user = admin_user()->toarray();
//        $arr_admin['user_id'] = $admin_user['rong_cloud_id'];
//        $arr_admin['name'] = $admin_user['username'];
//        $arr_admin['portrait_uri'] = oss_full_url('xinda/file/2020-01/15/200115041940_5e1ecb1c36c92.jpeg');
//        $arr_list = RongService::getToken( (array) $arr_admin );


        return view("admin::jpush.im");
    }

    /**
     *  获取平台信息
     *  hinq 2020 5 20
     *
     ***/
    public function get_platform_info(){
        $admin_user = admin_user()->toarray();
        $arr_admin['user_id'] = $admin_user['rong_cloud_id'];
        $arr_admin['name'] = $admin_user['username'];
        $arr_admin['portrait_uri'] = oss_full_url( $admin_user['head_img'] );
        $arr_list = RongService::getToken( (array) $arr_admin );
        $arr_list['data']['name'] = $admin_user['username'];
        $arr_list['data']['head_img'] = oss_full_url( $admin_user['head_img'] );
        $arr_list['data']['app_key'] = 'pwe86ga5p9wp6';
        return hello_success( 'ok' , $arr_list );
    }

/*** 下面的暂时未使用到******/

    //通过融云返回的信息获取对话的用户信息
    public function get_user_info(){
        $arr_input = (array) Request::all();
        $arr_users = [];
        foreach ( $arr_input as $key => $vo ){
            $arr_users = $vo;
        }

        $arr_option['field'] = ['id','username','nickname','head_img'];

        $user_info = \HiCommon\Repository\UsersRepository::get_one_by_whereIn((array)$arr_users, (array)$arr_option);   //获取指定的用户信息

        if( empty( $user_info->first() ) ){
            return hello_error('参数错误');
        }
        $user_info = $user_info->toarray();
        $head_img = $user_info['head_img']['full_src'];
        unset( $user_info['head_img'] );
        $user_info['image'] = oss_full_url( $head_img );

        return hello_success('ok' , $user_info );

    }


    public function get_im_list(){
        $bbb_list = [
            [
                "username"=>"贤心32432"
                ,"id"=> "100001"
                ,"avatar"=> "//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg"
                ,"sign"=> "这些都是测试数据，实际使用请严格按照该格式返回"
            ],[
                "username"=> "Z_子晴1234648"
                ,"id"=> "108101"
                ,"avatar"=> "//tva1.sinaimg.cn/crop.0.23.1242.1242.180/8693225ajw8fbimjimpjwj20yi0zs77l.jpg"
                ,"sign"=> "微电商达人"
            ],
            [
                "username"=> "贤心"
                ,"id"=> "100001"
                ,"avatar"=> "//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg"
                ,"sign"=> "这些都是测试数据，实际使用请严格按照该格式返回"
                ]
        ];
        $data['list'] = $bbb_list;
        return ['code'=>0,'msg'=>'','data'=>$data];
    }



    public function get_im_my(){

        $admin_user = admin_user()->toarray();
        $arr_admin['user_id'] = $admin_user['rong_cloud_id'];
        $arr_admin['name'] = $admin_user['username'];
        $arr_admin['portrait_uri'] = oss_full_url('xinda/file/2020-01/15/200115041940_5e1ecb1c36c92.jpeg');




        $list = [
            "mine"=> [
                "username" => $arr_admin['name']
              ,"id" => $arr_admin['user_id']
              ,"status" => "online"
              ,"avatar" => $arr_admin['portrait_uri']
            ],
             "friend"=> [
                 [
                    "groupname"=> "聊天列表"
                    ,"id"=> 0
                    ,"list"=> [
                        [
                            "username"=> "贤心"
                            ,"id"=> "100001"
                            ,"avatar"=> "//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg"
                            ,"sign"=> "这些都是测试数据，实际使用请严格按照该格式返回"
                            ,"status"=> "online"
                        ],[
                            "username"=> "刘小涛"
                            ,"id"=>"100001222"
                            ,"sign"=>"如约而至，不负姊妹欢乐颂"
                            ,"avatar"=> "//tva4.sinaimg.cn/crop.0.1.1125.1125.180/475bb144jw8f9nwebnuhkj20v90vbwh9.jpg"
                        ]
                    ]
                 ]
      ]
        ];



        return ['code'=>0,'msg'=>'','data'=>$list ];
    }


    /**
     *  通过融云id 获取用户信息
     *  hinq 2020 5 19
     *
     **/
    public function get_new_message(){
        $arr_input = (array) Request::all();
        $where['username'] = $arr_input['targetId'];
        $arr_option['field'] = ['id','nickname','head_img'];
        $arr_shop =\HiCommon\Repository\UsersRepository::get_one_where( $where , $arr_option);
        $arr_shop = $arr_shop->toarray();
        if( empty( $arr_input['content']) ){
            return hello_error('空消息');
        }
        $message = [
            'username'=>$arr_shop['nickname']
            ,'avatar'=> oss_full_url( $arr_shop['head_img']['full_src'] )
            ,'id'=> $arr_input['targetId']
            ,'type'=> "friend"
            ,'content'=> $arr_input['content']
            ,'timestamp'=> date('Y-m-d H:i:s' )
        ];

        return hello_success('ok', $message);
    }

}